<?php
/**
 * Created by PhpStorm.
 * User: Shinelon
 * Date: 2021/1/7
 * Time: 15:38
 */

namespace app\index\controller;


use app\BaseController;
use app\Common\Upload;
use app\index\model\AttachmentModel;
use think\App;

class File extends BaseController
{
    public function __construct(App $app)
    {
        parent::__construct($app);
        //上传者id
        $this->uid = 0;
    }

    /**
     * @return \think\Response
     * 单文件上传
     */
    public function uploadFile()
    {

        //获取参数
        $input = $this->_param;
        //获取文件列表
        $files  = $this->request->file('file');

//        dd($input,$files);

        //检查文件是否存在
        if(empty($files)) return $this->error('not file ,please check file.');
        //设置类型
        $type = 'picture';
        if(isset($input['type'])) $type = $input['type'];
        $result = [];
        //生成上传模型
        $uploda_model = new Upload();
        //上传文件
        $info = $uploda_model->upload($type ,$files);
        if(!empty($info))
        {
            //获取上传者id
            $info['uid'] = $this->uid;
            $info['attachment_path'] = 'http://'.getFilePath($info['attachment'] , $this->_host ,$info['driver']);
            $info['category_id'] = isset($input['category'])?$input['category']:0;
            $info['category_two_id'] = isset($input['category_two_id'])?$input['category_two_id']:0;
            $info['category_three_id'] = isset($input['category_three_id'])?$input['category_three_id']:0;
            //写入数据库
            $attachment_model = new AttachmentModel();
            $data = $attachment_model->createAttach($info);
            //判断写入数据库是否成功
            if(!empty($data)) $result[] = $info;
        }
        //数据处理
        return $this->success($result);
    }

    /**
     * @return \think\Response
     * 多文件上传
     */
    public function uploadFiles()
    {

        //获取参数
        $input = $this->_param;
        //获取文件列表
        $files  = $this->request->file('file');

        //检查文件是否存在
        if(empty($files)) return $this->error('not file ,please check file.');
        //设置类型
        $type = 'picture';
        if(isset($input['type'])) $type = $input['type'];
        $result = [];
        //生成上传模型
        $uploda_model = new Upload();
        foreach($files as $file)
        {
            //上传文件
            $info = $uploda_model->upload($type ,$file);
            if(!empty($info))
            {
                //获取上传者id
                $info['uid'] = $this->uid;

                $info['attachment_path'] = 'http://'.getFilePath($info['attachment'] , $this->_host,$info['driver']);
                $info['category_id'] = isset($input['category'])?$input['category']:0;
                $info['category_two_id'] = isset($input['category_two_id'])?$input['category_two_id']:0;
                $info['category_three_id'] = isset($input['category_three_id'])?$input['category_three_id']:0;
                $info['platform'] = 1;

                //写入数据库
                //判断写入数据库是否成功
                if(!empty($data)) $result[] = $info;
            }
        }
        //数据处理
        return $this->success($result);
    }

    //获取文件列表
    public function listFile()
    {
        $param = $this->_param;
        $filter = [];
        $filter[] = ['type','=',1] ;
        //判断分组是否存在
        if(isset($param['category_id']) && $param['category_id'] != '') {
            if($param['category_id'] == 0) {
                $filter[] = ['category_three_id', '=', 0];
                $filter[] = ['category_two_id', '=', 0];
                $filter[] = ['category_id', '=', 0];
            }else{
                $filter[] = ['category_id','=', $param['category_id']];
            }
        }
        if(isset($param['category_two_id']) && !empty($param['category_two_id'])) $filter[] = ['category_two_id', '=', $param['category_two_id']];
        if(isset($param['category_three_id']) && !empty($param['category_three_id'])) $filter[] = ['category_three_id' ,'=', $param['category_three_id']];
        //判断文件类型是否存在
        if(isset($param['type']) && in_array($param['type'], [1,2,3,'1','2','3'])) $filter[] = ['type', '=', $param['type']];
        //判断用户id是否存在
//      if(isset($this->uid)) return $this->error('not login,please login');
//      $filter['uid'] = $this->uid;
        //获取和生成分页信息
        if(isset($param['keyword'])){
            $filter[] = ['filename','like','%'.$param['keyword'].'%'];
        }
        $page_config = array(
            'page' => 1,
            'page_count' => 10
        );
        if(isset($param['page']) && $param['page'] > 0) $page_config['page'] = $param['page'];
        if(isset($param['limit']) && $param['limit'] > 0) $page_config['page_count'] = $param['limit'];
        //生成模型类
        $file_model = new AttachmentModel();
        //查询总数
        $count = $file_model->listAttachCount($filter);
        $files = $file_model->listAttach($filter ,$page_config);
//        $files = transImagesOne($files ,['attachment'] ,$this->_uniacid);
        foreach($files as $key => $val)
        {
            $files[$key]['attachment_path'] = 'http://'.getFilePath($val['attachment'] ,$this->_host ,$this->_uniacid);
        }
        //生成返回数据
        $page_config['total'] = $count;
        $page_config['total_page'] = (int)($page_config['total'] / $page_config['page_count']);
        if(($page_config['total'] % $page_config['page_count']) > 0) $page_config['total_page'] = $page_config['total_page'] + 1;
        $result = $page_config;
        $result['files'] = $files;
        //返回数据
        return $this->success($result);
    }

    //获取文件
    public function getFile()
    {
        $param = $this->_param;
        $filter['uniacid'] = $this->_uniacid;
        $filter['id']      = $param['id'];
        //生成模型
        $file_model = new AttachmentModel();
        //查询数据
        $file       = $file_model->getAttach($filter);
        if(isset($file['attachment']))
        {
            $file['attachment_path'] = 'http://'.getFilePath($file['attachment'] ,$this->_host ,$this->_uniacid);
        }
        //返回数据
        return $this->success($file);
    }

    //删除文件
    public function delFile()
    {
        //获取参数
        $data = $this->_param;
        //获取参数
        if(!isset($data['ids'])) return $this->error('not file id,please check.');
        //删除条件
        $ids = $data['ids'];
        $filter = [
            ['id','in',$ids],
            ['uniacid','=',$this->_uniacid]
        ];
        //生成模型
        $file_model = new AttachmentModel();
        //删除
        $result = $file_model->delAttach($filter);
        return $this->success($result);
    }

    //批量移动文件
    public function moveFile()
    {
        //获取参数
        $data = $this->_param;
        //获取参数
        if(!isset($data['ids'])) return $this->error('not file id,please check.');
        if(!isset($data['category_id'])) return $this->error('未获取到分组信息，请检查');
        //检查分组是否存在
        $attachment_category_model = new AttachmentCategoryModel();
        $attachmentCategory = $attachment_category_model->getAttachCategory(['id'=>$data['category_id']]);
        if(empty($attachmentCategory)) return $this->error('分组不存在，请检查');
        //批量移动的数据条件
        $ids = $data['ids'];
        $filter = [
            ['id','in',$ids],
            ['uniacid','=',$this->_uniacid]
        ];
        //移动到的分组id
        $update['category_id'] = $data['category_id'];
        $update['category_two_id'] = $data['category_two_id'];
        $update['category_three_id'] = $data['category_three_id'];
        //生成模型
        $file_model = new AttachmentModel();
        //删除
        $result = $file_model->moveAttach($filter,$update);
        return $this->success($result);
    }

}